<template>
  <div style="padding: 20px">
    <el-form :model="form" label-width="120">
      <el-form-item label="用户名">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" type="password"></el-input>
      </el-form-item>
      <el-form-item align="center">
        <el-button type="primary" @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      }
    }
  },
  methods: {
    login() {
      console.log(this.form)
      this.http.post('api/permission/getMenu', this.form).then(res => {
        console.log(res)
        res = res.data
        if (res.code === 20000) {
          this.$store.commit('clearMenu') // 清除之前存储的数据,避免二次登陆
          this.$store.commit('setMenu', res.data.menu) // 把新得到的数据传到vuex中
          this.$store.commit('setToken', res.data.token) // 获取后台返回的token id标识
          this.$store.commit('addMenu', this.$router) // 获取数据把值传给vuex,配置动态路由
          this.$router.push({ name: 'home' }) // 跳转首页
        } else {
          this.$message.warning(res.data.message) // 引入Element 给注册的全局对象，常用于主动操作后的反馈提示。
        }
      })
    }
  }
}
</script>

<style lang="stylus" scoped>
.el-form
  width: 50%;
  margin: auto;
  padding: 45px;
  height: 450px;
  background-color: #fff;
  .el-button--primary
    width: 50%
    margin-top 20px
</style>
